import {request, handleFetchResultNotification, REQUEST_TYPE} from '../utils';
import {formatMessage} from './intl';
import * as dto from './DTO';
import stringify from 'Shared/utils/stringify';

const baseUrl = '/sales/api/v1';

const getInitList = formatMessage({
    id: 'api.getInitList',
    defaultMessage: '查询初始数据'
});

const getListDesc = formatMessage({
    id: 'api.getList',
    defaultMessage: '查询信用证数据'
});

const getDetailDesc = formatMessage({
    id: 'api.getDetail',
    defaultMessage: '查询详情'
});

const newOrderDesc = formatMessage({
    id: 'api.newOrderDesc',
    defaultMessage: '新增'
});

const getReceiveDesc = formatMessage({
    id: 'api.getReceive',
    defaultMessage: '到款登记'
});

const exportDesc = formatMessage({
    id: 'api.exportData',
    defaultMessage: '导出'
});

const modifyOrderDesc = formatMessage({
    id: 'api.modifyOrderDesc',
    defaultMessage: '编辑'
});

const getCloseDesc = formatMessage({
    id: 'api.getClose',
    defaultMessage: '关闭'
});

const getApproveDesc = formatMessage({
    id: 'api.getApprove',
    defaultMessage: '审核'
});

const getSubmitDesc = formatMessage({
    id: 'api.getSubmit',
    defaultMessage: '提交'
});

const getFinanceApproveDesc = formatMessage({
    id: 'api.getFinanceApproveDesc',
    defaultMessage: '财务审核'
});
const draftDesc = formatMessage({
    id: 'api.draftDesc',
    defaultMessage: '草本转正本'
});
// 初始化查询
export const initQuery = () => request(`${baseUrl}/ui/letterOfCredit/init`, null, dto.InitData)
    .then(handleFetchResultNotification(REQUEST_TYPE.initQuery, getInitList));

// 查询信用证数据列表
export const getList = condition => {
    const queryString = stringify(condition);
    return request(`${baseUrl}/letterOfCredits?${queryString}`, null, dto.List)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getListDesc));
};

// 查询信用证数据详情
export const getDetail = id => request(`${baseUrl}/letterOfCredits/${id}`, null, dto.Detail)
    .then(handleFetchResultNotification(REQUEST_TYPE.query, getDetailDesc));

// 新增
export const newOrder = data => request(`${baseUrl}/letterOfCredits`, {
    method: 'POST',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, newOrderDesc));

// 编辑
export const modifyOrder = (id, data) => request(`${baseUrl}/letterOfCredits/${id}/`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, modifyOrderDesc));

// 到款登记修改
export const receiveData = (id, data) => request(`${baseUrl}/letterOfCredits/${id}/receive`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, getReceiveDesc));

// 关闭
export const closeOrder = (id, rowVersion) => request(`${baseUrl}/letterOfCredits/${id}/close`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify({
        rowVersion
    })
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, getCloseDesc));

//提交
export const submitOrder = (id, rowVersion) => request(`${baseUrl}/letterOfCredits/${id}/submit`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify({
        rowVersion
    })
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, getSubmitDesc));

//导出
export const exportDate = queryCondition => {
    const queryString = stringify(queryCondition);
    return request(`${baseUrl}/letterOfCredits/export?${queryString}`)
        .then(handleFetchResultNotification(REQUEST_TYPE.submit, exportDesc));
};

//导入模板
export const importTemp = () => request(`${baseUrl}/letterOfCredits/importTemp`)
    .then(handleFetchResultNotification(REQUEST_TYPE.query, null)).then(res => res.data);

//审核通过
export const approvePass = (id, data) => request(`${baseUrl}/letterOfCredits/${id}/approvePass`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, getApproveDesc));
//审核不通过
export const approveReject = (id, data) => request(`${baseUrl}/letterOfCredits/${id}/approveReject`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, getApproveDesc));

//财务审核通过
export const financePass = (id, data) => request(`${baseUrl}/letterOfCredits/${id}/financeApprovePass`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, getFinanceApproveDesc));

//财务审核不通过
export const financeReject = (id, data) => request(`${baseUrl}/letterOfCredits/${id}/financeApproveReject`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, getFinanceApproveDesc));

//草本转正本
export const draftData = (id, data) => request(`${baseUrl}/letterOfCredits/${id}/draft`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, draftDesc));

const getAreasDesc = formatMessage({
    id: 'api.message.getAreasDesc',
    defaultMessage: '查询国家'
});
export const getAreasSimple = condition => {
    const queryString = stringify(condition);
    return request(`${baseUrl}/areas/simple?${queryString}`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getAreasDesc));
};

export const getAreas = condition => {
    const queryString = stringify(condition);
    return request(`${baseUrl}/areas/query?${queryString}`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getAreasDesc));
};
// 查询最近一次信用证
export const getLastTime = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/letterOfCredits/lastTime?${queryString}`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getListDesc));
};

const getListHistoryDesc = formatMessage({
    id: 'api.getListHistoryDesc',
    defaultMessage: '查询历史信用证数据'
});

const getDetailHistoryDesc = formatMessage({
    id: 'api.getDetailHistoryDesc',
    defaultMessage: '查询历史详情'
});

// 查询历史信用证数据列表
export const getListHistory = condition => {
    const queryString = stringify(condition);
    return request(`${baseUrl}/letterOfCreditChangeHistories?${queryString}`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getListHistoryDesc));
};

// 查询历史信用证数据详情
export const getDetailHistory = id => request(`${baseUrl}/letterOfCreditChangeHistories/${id}`, null, null)
    .then(handleFetchResultNotification(REQUEST_TYPE.query, getDetailHistoryDesc));
